package com;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller

public class FileUploadController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取webapp真实目录
        String path = request.getServletContext().getRealPath("/static/upload/");
        //获取上传文件名称todo 一般上传文件会名称改为时间截 防止重名
        String fileName = file.getOriginalFilename();
        String filePath= path+"/"+fileName;
        if (! file.isEmpty()){
            file.transferTo(new File(filePath));
            return "uploadSuccess";
        }
        return "uploadFailure";
    }
}